//
//  MeetingGateway.swift
//  GOFM
//
//  Created by siri on 2024/10/22.
//  Copyright © 2024 Glad. All rights reserved.
//

import Combine
import Foundation
import Factory


// 定义网关协议
protocol IdleTimeGatewayProtocol {
    func getIdleTime() -> AnyPublisher<[IdleTime], Error>
}

// 定义网关实现类
struct IdleTimeGateway: IdleTimeGatewayProtocol {
    func getIdleTime() -> AnyPublisher<[IdleTime], Error> {
        Future<[IdleTime], Error> { promise in
            let meetings = [
                IdleTime(name: "iPhone", price: 999),
                IdleTime(name: "MacBook", price: 2999)
            ]
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                promise(.success(meetings))
            }
        }
        .eraseToAnyPublisher()
    }
}

struct PreviewIdleTimeGateway: IdleTimeGatewayProtocol {
    func getIdleTime() -> AnyPublisher<[IdleTime], Error> {
        Future<[IdleTime], Error> { promise in
            let meetings = [
                IdleTime(name: "iPhone", price: 999),
                IdleTime(name: "MacBook", price: 2999)
            ]
            
            promise(.success(meetings))
        }
        .eraseToAnyPublisher()
    }
}

extension Container {
    var idleTimeGateway: Factory<IdleTimeGatewayProtocol> {
        Factory(self) { IdleTimeGateway() }
            .onPreview {
                PreviewIdleTimeGateway()
            }
    }
}
